package ModelLayer;

import java.util.*;
/**
 * Write a description of class Container here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDContainer
{
    private ArrayList<DVD> dvdList;
    private String result;
    private String addResult;
    private String resultCopy;
    private static DVDContainer instance;
    private DVDContainer() 
    {
        dvdList = new ArrayList<DVD>();
    }
    
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
    
    public ArrayList<DVD> getDVDList()
    {
        return dvdList;
    }
    
    public void addDVDs(DVD dvd)
    {
        int index = 0;
        boolean found = false;
        while(index < dvdList.size() && !found)
        {
            if(dvd.getBarcode().equals(dvdList.get(index).getBarcode()))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            addResult = "There is a DVD with this barcode.";
        }
        else
        {
            dvdList.add(dvd);
            addResult = dvdList.get(index).getTitle() + " was added to DVDs.";
        }
    }
    
    public String addResult()
    {
        return addResult;
    }
    
    public void deleteDVDs(String barcode)
    {
        int index = 0;
        boolean found = false;
        while(index < dvdList.size() && !found)
        {
            if(dvdList.get(index).getBarcode().equals(barcode))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            result = dvdList.get(index).getTitle() + " was removed.";
            dvdList.remove(index);
        }
        else
        {
            result = "No DVD with that barcode.";
        }
    }
    
    public String deleteResult()
    {
        return result;
    }
    
    public String showDVDs(String barcode)
    {
        String result ="";
        String copy="";
        int index = 0;
        int index1 = 0;
        boolean found = false;
        while(index < dvdList.size() && !found) 
        {                                           
            if(dvdList.get(index).getBarcode().equals(barcode)) 
            {
                found = true;
                while(index1 < dvdList.get(index).getDvdCopyList().size())
                {
                    if(dvdList.get(index).getDvdCopyList().get(index1).getSerialNumber().length() == 6)
                    {
                        copy += "\n \nSerial number: " + dvdList.get(index).getDvdCopyList().get(index1).getSerialNumber() + "\nPurchase date: " 
                        + dvdList.get(index).getDvdCopyList().get(index1).getPurchaseDate() + "\nPurchase price: "
                        + dvdList.get(index).getDvdCopyList().get(index1).getPurchasePrice() + "$ \nState: "
                        + dvdList.get(index).getDvdCopyList().get(index1).getState();
                    }
                    index1++;
                }
            }
            else
            {
                index++;  
            }
         } 
        if (found == true)
        {
            result = "Title: " + dvdList.get(index).getTitle() + "\nArtist: "  
            + dvdList.get(index).getArtist() + "\nPublication date: " 
            + dvdList.get(index).getPublicationDate() + "\n \nCOPIES: " + copy;
        }
        else
        {   
            result = "No DVD with that barcode.";
        }
        return result;
    }
    
    public void updateDVDs(String oldBarcode, String newTitle, String newArtist, String newPublicationDate)
    {
        int index =0;
        while (index <dvdList.size())
        {
            if(dvdList.get(index).getBarcode().equals(oldBarcode))
            {
                if(newTitle =="")
                {
                }
                else 
                {
                    dvdList.get(index).setTitle(newTitle); 
                }
                if(newArtist =="")
                {
                }
                else 
                { 
                    dvdList.get(index).setArtist(newArtist);
                }
                if(newPublicationDate =="")
                { 
                }
                else 
                {
                    dvdList.get(index).setPublicationDate(newPublicationDate);
                }
            }
            index++;
        }   
    }
    
    public int sizeOfDVDList()
    {
        return dvdList.size();
    }
    
    //COPIES
    
    public void makeCopies(String barcode, String serialNumber, String purchaseDate, String purchasePrice)
    {
        int index = 0;
        boolean found = false;
        
        while(index < dvdList.size() && !found)
        {
            if(dvdList.get(index).getBarcode().equals(barcode))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        int i =0;
        int y =0;
        boolean found1 = false;
        boolean found2 = false;
        while(i < dvdList.size() && !found1)
        {
            if(dvdList.size() > 0)
            {
                y = 0;
                while(y < dvdList.get(i).getDvdCopyList().size() && !found2)
                {
                    if(dvdList.get(i).getDvdCopyList().get(y).getSerialNumber().equals(serialNumber))
                    {
                        found1 = true;
                        found2 = true;
                    }
                    else
                    {
                        y++;
                    }
                }
            }
            if(found1 == false)
            {
                i++;
            }
        }
        if(found == true && found2 == false)
        {
            dvdList.get(index).makeCopy(serialNumber, purchaseDate, purchasePrice);
            resultCopy = "A copy of " + dvdList.get(index).getTitle() + " was made.";
        }
        else
        {
            resultCopy = "Maybe there is a copy with that serial number. \nOr there is no dvd with that barcode.";
        }
    }
    
    public String resultCreateCopies()
    {
        return resultCopy;
    }
}

